import { BasicColumn, FormSchema } from "/@/components/Table"
import { render } from "/@/utils/common/renderUtils"
import { h } from "vue"
import { isHtml } from "/@/utils/common/htmlUtil"
import { DescItem } from "/@/components/Description/index"
import ToModalPage from "./ToModalPage.vue"
import { listDataGroupTree } from "/@/api/infra/dataGroup"
import { DataGroupBizType } from "/@/components/BizDataGroup/index"
import { useUserStore } from "/@/store/modules/user"

const userStore = useUserStore()

export const columns: BasicColumn[] = [
  {
    title: "租户名称",
    dataIndex: "tenantName",
    width: 150,
  },
  {
    title: "消息标题",
    dataIndex: "title",
    width: 150,
  },

  {
    title: "消息编码",
    dataIndex: "noticeCode",
    width: 150,
    slot: "notice",
  },

  {
    title: "渠道类型",
    dataIndex: "channelTypeName",
    width: 90,
    slot: "channel",
  },

  {
    title: "渠道名称",
    dataIndex: "channelName",
    width: 90,
  },
  {
    title: "推送状态",
    dataIndex: "status",
    width: 80,
    customRender: ({ text }) => {
      const color = text == "2" ? "red" : "green"
      return render.renderTag(render.renderDict(text, "message_push_status"), color)
    },
  },
  {
    title: "重试次数",
    dataIndex: "retryCount",
    width: 80,
  },
  {
    title: "接受用户",
    dataIndex: "toUser",
    width: 150,
  },
  {
    title: "推送时间",
    dataIndex: "pushTime",
    width: 160,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: "租户",
    field: "tenantId",
    component: "ApiSelect",
    ifShow: ({ values, model }) => {
      return userStore.getSelectTenant.isMainTenant
    },
    colProps: { span: 8 },
  },
  {
    label: "接受用户",
    field: "toUser",
    componentProps: {
      placeholder: "手机号/邮箱/openId等等",
    },
    component: "Input",
  },
  {
    label: "渠道",
    field: "channelType",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "message_channel_type",
      stringToNumber: false,
      showSearch: true,
      placeholder: "全部",
      chooseOptionLabel: "全部",
    },
    colProps: { span: 6 },
  },
  {
    label: "推送状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "message_push_status",
      placeholder: "全部",
      chooseOptionLabel: "全部",
      stringToNumber: false,
    },
    colProps: { span: 4 },
  },
  {
    label: "功能组",
    field: "groupId",
    component: "ApiSelect",
    componentProps: {
      api: listDataGroupTree,
      params: {
        bizType: DataGroupBizType.MSG_PUSH_TASK,
      },
      allowClear: true,
      optionFilterProp: "label",
      showSearch: true,
      labelField: "name",
      valueField: "id",
      placeholder: "全部",
    },
    colProps: { span: 6 },
  },
  {
    field: "fieldTime",
    component: "RangePicker",
    label: "时间范围",
    componentProps: {
      valueType: "Date",
    },
    colProps: {
      span: 8,
    },
  },
]

export const pushDetailSchema: DescItem[] = [
  {
    label: "消息标题",
    field: "title",
  },
  {
    field: "noticeCode",
    label: "消息编码",
    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
  {
    field: "channelTypeName",
    label: "渠道类型",
    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
  {
    field: "channelName",
    label: "渠道名称",
    render: (curVal, data) => {
      return render.renderTag(curVal, "green")
    },
  },
  {
    field: "bizKey",
    label: "业务主键",
    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
  {
    field: "bizType",
    label: "业务类型",
    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
  {
    field: "pushTime",
    label: "推送时间",
    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
  {
    field: "toUser",
    label: "接收用户",

    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
  {
    field: "retryCount",
    label: "重试次数",
    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
  {
    field: "status",
    label: "推送状态",
    span: 2,
    render: (curVal, data) => {
      const color = curVal == "2" ? "red" : "green"
      return render.renderTag(render.renderDict(curVal, "message_push_status"), color)
    },
  },
  {
    label: "页面地址",
    field: "pageUrl",
    span: 2,
  },
  {
    field: "content",
    label: "消息内容",
    span: 4,
    render: (curVal, data) => {
      if (isHtml(curVal)) {
        return h(ToModalPage, { content: curVal })
      }
      return render.renderSpan(curVal)
    },
  },
  {
    field: "msgFields",
    label: "消息字段",
    span: 2,
    render: (curVal, data) => {
      return render.renderSpan(JSON.stringify(curVal))
    },
  },
  {
    field: "pushResult",
    label: "推送结果",
    span: 2,
    render: (curVal, data) => {
      return render.renderSpan(curVal)
    },
  },
]
